#!/bin/sh /etc/rc.common

START=15

NAME="cpufreq"
CPUFREQ_PATH="/sys/devices/system/cpu/cpufreq"

write_cpufreq_config() {
	local value
	config_get value "$NAME" "$1"
	[ -z "$value" ] || echo -n "$value" > "$2"
}

start() {
	config_load "$NAME"

	for i in $(ls -d "$CPUFREQ_PATH"/policy[0-9]* 2>"/dev/null" | grep -Eo "[0-9]*$")
	do
		[ -z "$(config_get "$NAME" "governor$i")" ] && return

		write_cpufreq_config "governor$i" "$CPUFREQ_PATH/policy$i/scaling_governor"
		write_cpufreq_config "minfreq$i" "$CPUFREQ_PATH/policy$i/scaling_min_freq"
		write_cpufreq_config "maxfreq$i" "$CPUFREQ_PATH/policy$i/scaling_max_freq"
		if [ "$(config_get "$NAME" "governor$i")" = "ondemand" ]; then
			write_cpufreq_config "sdfactor$i" "$CPUFREQ_PATH/ondemand/sampling_down_factor"
			write_cpufreq_config "upthreshold$i" "$CPUFREQ_PATH/ondemand/up_threshold"
		fi
	done
}
